<!--
* description: 客户新增
* author: zxuan
* createdDate: 2019-11-11
-->
<template>
  <section class="carTypeConfig">
    <el-dialog v-dialogDrag
      title="新增客户"
      :visible.sync="addPopupsVisible"
      width="1000px"
      center
      @close="sendCode"
      :close-on-click-modal="false"
      :append-to-body="true"
    >
      <dialogHeader
        slot="title"
        :title="'新增客户'"
        :styleType="$store.state.app.dialogType"
      ></dialogHeader>
      <one-table-template
        ref="multipleTable"
        :dynamicButtons="tableButtons"
        :dynamicComponents="tableComponents"
        :dynamicApiConfig="apiConfig"
        :dynamicTableCols="tableCols"
        :dynamicFormFields="formField"
        :dynamicIsShowSelect="isMul"
      />
    </el-dialog>
  </section>
</template>
<script>
import { oneTableWithViewTemplateMixins } from "@/components/mixins/oneTableWithViewTemplateMixins";
import { valueObjectMixins } from "@/components/mixins/valueObjectMixins";
import { crmApis } from "@/api/apiList/crm";
import { requestAPI } from "@/api/commonRequest";
import OneTableTemplate from "@/components/crm/commonComponents/crmPopupsOneTable";
export default {
  // 组件混入对象：{data[listQuery] methods[queryTable]}
  mixins: [oneTableWithViewTemplateMixins, valueObjectMixins],
  components: {
    OneTableTemplate,
    dialogHeader: () => import("@/components/dialogHeader") //弹窗标题
  },
  data() {
    return {
      addPopupsVisible: false,
      // 网格查询API配置对象
      apiConfig: crmApis.mdmDlrCustInfoQueryGetCustList, //专营店客户查询
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: "btnKey1",
          type: "primary",
          size: "small",
          clickEvent: () => this.save(),
          text: "保存"
        },
        {
          compKey: "btnKey2",
          type: "",
          size: "small",
          clickEvent: () => this.sendCode("custName", "custName"),
          text: "确认"
        }
      ],
      // 动态组件-查询条件
      tableComponents: [
        // 显示组件
        {
          compKey: "compKey1",
          labelName: "客户名称",
          codeField: "custName",
          component: () => import("@/components/crm/textbox/crmTextInput"),
          isRequire: true,
          type: "inputText",
          isMust: true
        },
        {
          compKey: "compKey2",
          labelName: "电话",
          codeField: "phone",
          isRequire: true,
          component: () => import("@/components/crm/textbox/crmTextInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "compKey3",
          labelName: "性别",
          codeField: "genderCode",
          lookuptype: "DB0022",
          component: () => import("@/components/org/LookupValue"),
          type: "dropdownList",
          isMust: true,
          isMul: false
        },
        {
          compKey: "compKey4",
          labelName: "生日",
          codeField: "birthDate",
          component: () => import("@/components/crm/Time/datePicker"),
          format: "yyyy-MM-dd",
          dateType: "datetime",
          isMust: true
        },
        {
          compKey: "compKey5",
          labelName: "身份证",
          codeField: "credNo",
          component: () => import("@/components/crm/textbox/crmTextInput"),
          type: "inputText",
          isMust: true
        },

        {
          compKey: "compKey6",
          labelName: "家庭电话",
          codeField: "homeTel",
          component: () => import("@/components/crm/textbox/crmTextInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "compKey7",
          labelName: "办公电话",
          codeField: "officeTel",
          component: () => import("@/components/crm/textbox/crmTextInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "compKey8",
          labelName: "省份",
          codeField: "provinceId",
          textField: "provinceName",
          isRequire: false,
          isMust: true,
          isMul: false,
          lookuptype: crmApis.mdmOrgProvinceQueryFindAll,
          changeCode: this.changeCityByProvince,
          component: () => import("@/components/crm/lookupValue/singleDropList")
        },
        {
          compKey: "provinceId",
          labelName: "城市",
          codeField: "cityId",
          textField: "cityName",
          isRequire: false,
          isMust: true,
          isMul: false,
          lookuptype: crmApis.mdmOrgCityQueryFindAll,
          changeCodeArray: [{ changCode: "provinceId" }],
          component: () => import("@/components/crm/lookupValue/secondDropList")
        },
        {
          compKey: "cityId",
          labelName: "区县",
          codeField: "countyId",
          textField: "countyName",
          isRequire: false,
          isMust: true,
          isMul: false,
          lookuptype: crmApis.mdmOrgCommunityQueryFindAll,
          changeCodeArray: [{ changCode: "cityId" }],
          component: () => import("@/components/crm/lookupValue/secondDropList")
        },
        {
          compKey: "compKey9",
          labelName: "邮政编码",
          codeField: "addrZip",
          component: () => import("@/components/crm/textbox/crmTextInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "compKey10",
          labelName: "详细地址",
          codeField: "addrStreet",
          component: () => import("@/components/crm/textbox/crmTextInput"),
          type: "inputText",
          inputType: "textarea",
          isMust: true
        }
      ],
      // 动态生成网格列
      tableCols: [
        {
          prop: "custName",
          label: "客户姓名",
          width: null,
          align: "center"
        },
        {
          prop: "phone",
          label: "电话",
          width: null,
          align: "center"
        },
        {
          prop: "genderCode",
          label: "性别code",
          width: null,
          hidden: true,
          align: "center"
        },
        {
          prop: "genderName",
          label: "性别",
          width: null,
          align: "center"
        },
        {
          prop: "addrProvince",
          label: "省份",
          width: null,
          align: "center"
        },
        {
          prop: "addrCity",
          label: "城市",
          width: null,
          align: "center"
        },
        {
          prop: "addrCounty",
          label: "区县",
          width: null,
          align: "center"
        },
        {
          prop: "dlrCustNo",
          label: "专营店客户编号",
          width: null,
          hidden: true,
          align: "center"
        }
      ],
      formField: {
        custName: "",
        addrCity: "",
        countyId: "",
        cityId: "",
        addrStreet: "",
        addrZip: "",
        addrCounty: "1",
        addrProvince: "",
        provinceName: "",
        cityName: "",
        countyName: "",
        provinceId: "",
        birthDate: "",
        credNo: "",
        custClassCode: "0", //客户类型
        credTypeCode: "1", //证件类型
        credTypeName: "身份证", //证件类型名称
        genderCode: "",
        homeTel: "",
        officeTel: "",
        phone: ""
      },
      handleTableRowData: {
        value: "custName",
        text: "custName"
      },
      params: {},
      ORG_TYPE: this.$store.getters.orgInfo.ORG_TYPE
    };
  },
  mounted() {},
  methods: {
    save() {
      let that = this;
      let flag = false;
      this.params = this.formField;
      this.params.addrProvince = this.params.provinceName;
      this.params.provinceName = undefined;
      this.params.addrCity = this.params.cityName;
      this.params.cityName = undefined;
      this.params.addrCounty = this.params.countyName;
      this.params.countyName = undefined;
      var config = this.config;
      config = {
        custName: [
          true,
          this.$crmcf.crmRegExp("empty"),
          "value",
          that.formField.custName,
          "客户名称",
          "不能为空"
        ],
        phone: [
          true,
          this.$crmcf.crmRegExp("empty"),
          "value",
          that.formField.phone,
          "联系电话",
          "不能为空",
          ""
        ]
      };
      this.$crmcf.validForm(that, "form", config, function(flag) {
        if (flag) {
          let queryObj = {
            type: "mutation",
            // api配置
            apiConfig: crmApis.createdOneIdMutationSaveCust, //专营店新建客户
            // 需要调用的API服务配置
            apiServices: [
              {
                //表格中台返回网格的字段
                apiQueryRow: ["dlrCustNo"]
              }
            ],
            //条件/实体参数（变量），根据typeParam中的定义配置
            variables: {
              pageSize: 10,
              pageIndex: 1,
              //当前中台使用的名称有dataInfo、info，具体查看API文档
              dataInfo: {
                custInfo: that.params,
                custModifyScence:
                  that.ORG_TYPE == "0"? "201" : "301"
              }
            }
          };
          queryObj.variables.dataInfo.custInfo.custFullName =
            that.formField.custName;
          queryObj.variables.dataInfo.custInfo.custClassCode = "0";
          queryObj.variables.dataInfo.custInfo.credTypeCode = "1";
          queryObj.variables.dataInfo.custInfo.credTypeName = "身份证";
          //转换了中台请求格式数据
          var paramD = that.$getParams(queryObj);
          // 调用中台API方法（可复用）
          requestAPI(paramD).then(response => {
            if (response.result == "1") {
              that.$refs.multipleTable.formField = {};
              that.$refs.multipleTable.formField.dlrCustNo =
                response.rows.dlrCustNo;
              that.$crmcf.showMessages(that, "success", "保存成功！");
              that.queryTable(1);
            } else {
              that.$crmcf.showMessages(
                that,
                "error",
                response.msg
              );
            }
          });
        }
      });
    }
  }
};
</script>
